//
// Created by xili on 2024/8/14 20:53.
// GO BIG OR GO HOME.
//
#include "leetcode.h"

class Solution {
public:
    string arrangeWords(string text) {
        text[0] = tolower(text[0]);
        vector <string> vs;
        int n = text.size();
        int l = 0, r = 0;
        while (r < n) {
            while (r < n && text[r] != ' ') {
                r++;
            }
            vs.push_back(text.substr(l, r++ - l));
            l = r;
        }
        stable_sort(vs.begin(), vs.end(), [](string a, string b) { return a.size() < b.size(); });
        string ans;
        for (auto &item: vs) {
            ans += item + ' ';
        }
        ans.erase(ans.end() - 1);
        ans[0] = toupper(ans[0]);
        return ans;
    }
};